home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / gopher+1.2b4 / gopherd / tix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-09  |  5.4 KB  |  315 lines

  1. /********************************************************************
  2.  * lindner
  3.  * 3.2
  4.  * 1993/04/10 06:08:36
  5.  * /home/mudhoney/GopherSrc/CVS/gopher+/gopherd/tix.c,v
  6.  * Exp
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992, 1993 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: tix.c
  14.  * Routines to grant and validate tickets.
  15.  *********************************************************************
  16.  * Revision History:
  17.  * tix.c,v
  18.  * Revision 3.2  1993/04/10  06:08:36  lindner
  19.  * Randomizer fixes
  20.  *
  21.  * Revision 3.1  1993/03/19  20:00:19  lindner
  22.  * New TIX object
  23.  *
  24.  *
  25.  *********************************************************************/
  26.  
  27.  
  28. #include "tix.h"
  29. #include "Malloc.h"
  30. #include "String.h"
  31.  
  32. extern boolean DEBUG;
  33.  
  34. TixObj *
  35. TIXnew()
  36. {
  37.      TixObj *temp;
  38.      int i;
  39.  
  40.      temp = (TixObj *) malloc(sizeof(TixObj));
  41.      
  42.      for (i=0; i<8; i++)
  43.       temp->password[i] = ' ';
  44.      temp->password[8] = '\0';
  45.      
  46.      temp->username = STRnew();
  47.      temp->ticket   = 0;
  48.      
  49.      return(temp);
  50. }
  51.      
  52. void
  53. TIXdestroy(tix)
  54.   TixObj *tix;
  55. {
  56.      STRdestroy(tix->username);
  57.      
  58.      free(tix);
  59. }
  60.  
  61. void
  62. TIXinit(tix)
  63.   TixObj *tix;
  64. {
  65.      int i;
  66.  
  67.      STRinit(tix->username);
  68.  
  69.      for (i=0; i<8; i++)
  70.       tix->password[i] = ' ';
  71.      tix->password[8] = '\0';
  72.  
  73.      tix->ticket = 0;
  74. }
  75.  
  76. void
  77. TIXcpy(dest, orig)
  78.   TixObj *dest, *orig;
  79. {
  80.      STRcpy(dest->username, orig->username);
  81.      strncpy(dest->password, orig->password,8);
  82.      dest->ticket = orig->ticket;
  83. }
  84.  
  85.  
  86. /*
  87.  * Okay, we're done with the old ticket, go on to the next
  88.  */
  89.  
  90. void
  91. TIXusedTicket(tix)
  92.   TixObj *tix;
  93. {
  94.      tix->ticket++;
  95. }
  96.  
  97.  
  98.  
  99. char *
  100. TIXgetCryptedTicket(tix)
  101.   TixObj *tix;
  102. {
  103.      char crypted[17];
  104.      char cleardec[9];
  105.      char clearhex[17];
  106.      char password[17];
  107.      char *cp;
  108.      long out[3];
  109.      
  110.      sprintf(cleardec, "%-8d", TIXgetPlainTicket(tix));
  111.      Hexall(cleardec, clearhex);
  112.      Hexall(TIXgetPass(tix),password);
  113.  
  114.      if (DEBUG)
  115.       printf("Encrypting %s (%s) with key %s (%s)\n",
  116.          cleardec, clearhex, TIXgetPass(tix), password);
  117.      
  118.      encrypt(clearhex, password, out);
  119.      sprintf(crypted, "%.8lX%.8lX", out[0], out[1]);
  120.  
  121.      return(strdup(crypted));
  122. }
  123.  
  124.  
  125. /*
  126.  * Set up a random ticket
  127.  */
  128.  
  129. void      
  130. TIXrandomTicket(tix)
  131.   TixObj *tix;
  132. {
  133.      int t;
  134.      
  135.      srand(time(NULL));
  136.      t = rand();
  137.  
  138.      while (t > 88888888)
  139.       t /= 10;
  140.      
  141.      if (DEBUG)
  142.       printf("T is %ld\n", t);
  143.  
  144.      TIXsetPlainTicket(tix, t);
  145.      if (DEBUG) {
  146.       printf("Using default encrypted tix value 1520\n");
  147.       TIXsetPlainTicket(tix, 1520);
  148.      }
  149. }
  150.  
  151.  
  152. boolean
  153. TIXvalid(tix, crypted)
  154.   TixObj *tix;
  155.   char   *crypted;
  156. {
  157.      char cleardec[9];  /** Ticket in ascii **/
  158.      char clearhex[17]; /** ticket in hex   **/
  159.      char tixfromnet[17];
  160.      char password[17];
  161.      char *cp;
  162.      long out[3];
  163.      
  164.      sprintf(cleardec, "%-8d", TIXgetPlainTicket(tix));
  165.      Hexall(cleardec, clearhex);
  166.      Hexall(TIXgetPass(tix),password);
  167.      
  168.      if (DEBUG) {
  169.       printf("Testing user %s, pass: %s (%s), ticket %d (%s)\n",
  170.          TIXgetUser(tix),TIXgetPass(tix), password,
  171.          TIXgetPlainTicket(tix), clearhex);
  172.      }
  173.  
  174.      
  175.      decrypt(crypted, password, out);
  176.      sprintf(tixfromnet, "%.8lX%.8lX", out[0], out[1]);
  177.  
  178.      if (DEBUG)
  179.       printf("Decrypted ticket from net is: %s\n", tixfromnet);
  180.  
  181.      if (strncmp(tixfromnet, clearhex,8) == 0)
  182.       return(TRUE);
  183.      else
  184.       return(FALSE);
  185. }     
  186.  
  187.  
  188. void
  189. TIXtoFile(tix, fd)
  190.   TixObj *tix;
  191.   int    fd;
  192. {
  193.      char outputline[512];
  194.  
  195.      sprintf(outputline, "%s:%s:%d\n", TIXgetUser(tix), TIXgetPass(tix),
  196.          TIXgetPlainTicket(tix));
  197.  
  198.      writestring(fd, outputline);
  199. }
  200.  
  201.  
  202. boolean
  203. TIXfromFile(tix, fd)
  204.   TixObj *tix;
  205.   int    fd;
  206. {
  207.      char inputline[512];
  208.      int  numbytes;
  209.      char *pass, *tick;
  210.  
  211.      numbytes = readline(fd, inputline, sizeof(inputline));
  212.      if (numbytes <3)
  213.       return(FALSE);
  214.      
  215.      pass = strchr(inputline, ':');
  216.      if (pass == NULL)
  217.       return(FALSE);
  218.      *pass = '\0';
  219.      pass++;
  220.  
  221.      tick = strchr(pass, ':');
  222.      if (tick == NULL)
  223.       return(FALSE);
  224.      *tick = '\0';
  225.      tick++;
  226.  
  227.      TIXsetUser(tix, inputline);
  228.      TIXsetPass(tix, pass);
  229.      TIXsetPlainTicket(tix, atoi(tick));
  230.  
  231.      return(TRUE);
  232. }
  233.  
  234.  
  235.  
  236. /**************************
  237.  * TIXA stuff follows
  238.  */
  239.  
  240. boolean
  241. TIXAfromFile(tixa, fd)
  242.   TixArray *tixa;
  243.   int      fd;
  244. {
  245.      TixObj *tix;
  246.  
  247.      tix = TIXnew();
  248.  
  249.      while (1) {
  250.       if (TIXfromFile(tix, fd) == FALSE)
  251.            break;
  252.       
  253.       TIXAadd(tixa, tix);
  254.       TIXinit(tix);
  255.      }
  256.      if (TIXAgetNumEntries(tixa) == 0)
  257.       return(FALSE);
  258.      else
  259.       return(TRUE);
  260. }
  261.  
  262. void
  263. TIXAtoFile(tixa, fd)
  264.   TixArray *tixa;
  265.   int fd;
  266. {
  267.      int i;
  268.  
  269.      for (i=0; i<TIXAgetNumEntries(tixa); i++)
  270.       TIXtoFile(TIXAgetEntry(tixa, i), fd);
  271. }
  272.  
  273.  
  274.  
  275. int
  276. TIXAsearchUser(tixa, username)
  277.   TixArray *tixa;
  278.   char *username;
  279. {
  280.      int i;
  281.  
  282.      for (i=0; i<TIXAgetNumEntries(tixa); i++) {
  283.       char *tixuser  = TIXgetUser(TIXAgetEntry(tixa,i));
  284.  
  285.       if (tixuser == NULL)
  286.            return(-1);
  287.  
  288.       if (strcmp(username, tixuser) == 0)
  289.            return(i);
  290.      }
  291.      /** didn't find it **/
  292.      return(-1);
  293. }
  294.      
  295.  
  296. boolean 
  297. TIXAvalid(tixa, username, cryptedticket)
  298.   TixArray *tixa;
  299.   char     *username;
  300.   char     *cryptedticket;
  301. {
  302.      int i;
  303.      boolean status;
  304.  
  305.      i = TIXAsearchUser(tixa, username);
  306.      if (i<0)
  307.       return(FALSE);
  308.  
  309.      status = TIXvalid(TIXAgetEntry(tixa, i), cryptedticket);
  310.  
  311.      return(status);
  312. }
  313.  
  314.  
  315.